package com.cairenhui.trust.auto.task.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cairenhui.trust.auto.task.dao.CrmSaleProjectVMapper;
import com.cairenhui.trust.auto.task.dao.ProjectsMapper;
import com.cairenhui.trust.auto.task.model.CrmSaleProjectV;
import com.cairenhui.trust.auto.task.model.CrmSaleProjectVExample;
import com.cairenhui.trust.auto.task.model.Page;
import com.cairenhui.trust.auto.task.model.Projects;
import com.cairenhui.trust.auto.task.model.ProjectsExample;
import com.cairenhui.trust.auto.task.service.TaskService;
import com.cairenhui.trust.auto.task.util.DateUtils;
import com.cairenhui.trust.auto.task.util.constant.StatusConstants;


/**
 * 
 * @author 黄瑞庆
 * TODO CRM营销项目同步任务
 * 2017年2月27日
 */
@Component
public class CrmSaleProjectsTaskService extends TaskService{
	
	/**self4j */
	private Logger logger = LoggerFactory.getLogger(CrmSaleProjectsTaskService.class);
	@Autowired
	private CrmSaleProjectVMapper  crmSaleProjectVMapper;
	@Autowired
	private ProjectsMapper projectsMapper;
	
	@Override
	public void sysCrm() {
		//每次查询条数 QUERY_NUM
		//获取营销项目总数 TOTAL_COUNT
		CrmSaleProjectVExample crmSaleProjectVExample = new CrmSaleProjectVExample();
		TOTAL_COUNT = crmSaleProjectVMapper.countByExample(crmSaleProjectVExample);
		//根据每页查询数 ，总条数 计算出 总页数
		Page<CrmSaleProjectV> pageTemp = new Page<CrmSaleProjectV>(1, QUERY_NUM,TOTAL_COUNT);
		//开启分页查询
		for(int i = 1 ; i<= pageTemp.getTotalPage() ; i++){
			List<Projects> insertList = new ArrayList<Projects>();
			List<Projects> updateList = new ArrayList<Projects>();
			
			//同步时间
			String sysTime = DateUtils.getStrDate(new Date(), DateUtils.STRDATE_1+DateUtils.HOURS_MINUTES_SECONDS);
			
			Page<CrmSaleProjectV> page = new Page<CrmSaleProjectV>(1, QUERY_NUM,TOTAL_COUNT);
			crmSaleProjectVExample.setPage(page);
			List<CrmSaleProjectV> listSaleProject  = crmSaleProjectVMapper.selectByExample(crmSaleProjectVExample);
			
			//查询本地营销项目
			List<String> mapProjectCode = new ArrayList<String>();
			for(CrmSaleProjectV crmSaleProject : listSaleProject){
				mapProjectCode.add(crmSaleProject.getlProserialno());
			}
			
			ProjectsExample projectsExample = new ProjectsExample();
			projectsExample.createCriteria().andProjectCodeIn(mapProjectCode);
			List<Projects> listProject = projectsMapper.selectByExampleWithBLOBs(projectsExample);  
			
			Map<String,Object> mapProject = new HashMap<String, Object>();
			for(Projects project : listProject){
				mapProject.put(project.getProjectCode(),project);
			}
			
			for(CrmSaleProjectV crmSaleProject : listSaleProject){
				
				Projects project = (Projects) mapProject.get(crmSaleProject.getlProserialno());
				if(project == null){
					project = new Projects();
				}
				// 校验数据
				// 项目代码
				project.setProjectCode(crmSaleProject.getlProserialno());
				// 项目名称
				project.setProjectName(crmSaleProject.getcName());
				// 项目类型 销售项目
				project.setProjectType(StatusConstants.SALES_PROJECT);
				// 销售渠道 对所有
				project.setSalesChannel(StatusConstants.BOTH);
				// 项目状态
				project.setStatus(StatusConstants.CREATED);
				project.setContractRate(crmSaleProject.getfScale());
				//项目风险等级
				project.setProjectLevel(crmSaleProject.getcRisklevel());
				//项目收益类型
				project.setIncomeType(crmSaleProject.getcIncomedistribution());
				project.setRelatedOrgName(crmSaleProject.getcBusinessdepartment());
				
				if (null == project.getProjectId()) {
					project.setCreateTime(new Date());
					project.setUpdateTime(new Date());
					insertList.add(project);
				} else {
					project.setUpdateTime(new Date());
					updateList.add(project);
				}
			}
			
			if (updateList.size() > 0) {
				inserOrUpdate(updateList,false);
			}
			if (insertList.size() > 0) {
				inserOrUpdate(insertList,true);
			}
			logger.info("营销项目信息>>>>>>第{}次同步，--->同步条数{}，同步时间{}",i,listSaleProject.size(),sysTime);
			
		}
		
	}

	/**
	 * 
	  * TODO 循环插入 或 更新
	 * @author 黄瑞庆
	 * void
	 * @param list 出入对象集合
	 * @param isInsert true:插入
	 * 2017年3月3日 下午12:24:01
	 */
	public void inserOrUpdate(List<Projects> list , Boolean isInsert){
		for(Projects project : list){
			if(isInsert){
				projectsMapper.insertSelective(project);
			}else{
				projectsMapper.updateByPrimaryKeySelective(project);
			}
		}
	}
	
	
}
